package dao;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Repository;

@Repository
public class NfcTagDaoImpl implements NfcTagDao {
	private static SqlSessionFactory factory ; 
	private SqlSession session ; 
	static {
		System.out.println("------------------- mybatis loading --------------------"); 
		try{
			factory = new SqlSessionFactoryBuilder().build(
					Resources.getResourceAsReader("resource/mybatis/config/configuration.xml"));
		}catch(Exception e) {
			e.printStackTrace() ;
		}
	}
	
	public NfcTagDaoImpl() {
		session = factory.openSession(true) ;
	}
	
	@Override
	public String selectMid(String phoneId) {
		return session.selectOne("nfctag.selectMid", phoneId);
	}
	
	@Override
	public int calculateTimeTerm(HashMap<String, String> timeTerm) {
		System.out.println(timeTerm);
		return session.selectOne("nfctag.calculateTimeTerm", timeTerm);
	}
	
	@Override
	public HashMap<String, String> selectLatestNfcTagInfo(HashMap<String, String> nfcTagInfo) {
		return session.selectOne("nfctag.selectLatestNfcTagInfo", nfcTagInfo);
	}

	@Override
	public int insertNfcTagInfo(HashMap<String, String> nfcTagInfo) {
		return session.insert("nfctag.insertNfcTagInfo", nfcTagInfo);
	}
	
	@Override
	public int setStart(HashMap<String, String> nfcTagInfo) {
		return session.insert("nfctag.setStart", nfcTagInfo);
	}


	@Override
	public int selectFloor(String fnfcid) {
		return session.selectOne("nfctag.selectFloor", fnfcid);
	}

	@Override
	public int checkAddMemberFloor(HashMap<String, String> checkAddMemberFloorMap) {
		return session.selectOne("nfctag.checkAddMemberFloor", checkAddMemberFloorMap);
	}

	@Override
	public void insertMemberFloor(HashMap<String, String> checkAddMemberFloorMap) {
		session.insert("nfctag.insertMemberFloor", checkAddMemberFloorMap);
	}
	
	@Override
	public int updateMemberFloor(HashMap<String, String> memberFloorMap) {
		return session.update("nfctag.updateMemberFloor", memberFloorMap);
	}

	@Override
	public double selectAdditionalTemperature(String mid) {
		return session.selectOne("nfctag.selectAdditionalTemperature", mid);
	}

	@Override
	public void updateTemperature(HashMap<String, Object> temperatureMap) {
		session.update("nfctag.updateTemperature", temperatureMap);
	}

	@Override
	public Double selectTemperature(String mid) {
		return session.selectOne("nfctag.selectTemperature", mid);
	}

	@Override
	public void resetTemperature(String mid) {
		session.update("nfctag.resetTemperature", mid);
	}

	@Override
	public void updateRedbean(String mid) {
		session.update("nfctag.updateRedbean",mid);
	}

	@Override
	public List<HashMap<String, String>> mission1List(String mid) {
		return session.selectList("nfctag.mission1List", mid);
	}

	@Override
	public int calculateDateTerm(HashMap<String, String> dateTerm) {
		return session.selectOne("nfctag.calculateDateTerm", dateTerm);
	}

	@Override
	public int selectMission1Progress(String mid) {
		return session.selectOne("nfctag.selectMission1Progress", mid);
	}

	@Override
	public void insertMission1(String mid) {
		session.insert("nfctag.insertMission1", mid);
	}

	@Override
	public void updateMission1Progress(HashMap<String, Object> progressMap) {
		session.update("nfctag.updateMission1Progress", progressMap);
	}

	@Override
	public int selectMission2Progress(String mid) {
		return session.selectOne("nfctag.selectMission2Progress", mid);
	}

	@Override
	public void insertMission2(String mid) {
		session.update("nfctag.insertMission2", mid);
	}

	@Override
	public void updateMission2Progress(HashMap<String, Object> progressMap) {
		session.update("nfctag.updateMission2Progress", progressMap);
	}

	@Override
	public int selectDeptStairs(String mid) {
		return session.selectOne("nfctag.selectDeptStairs", mid);
	}

	@Override
	public int selectMission3Progress(String mid) {
		return session.selectOne("nfctag.selectMission3Progress", mid);
	}

	@Override
	public void insertMission3(String mid) {
		session.update("nfctag.insertMission3", mid);
		
	}

	@Override
	public void updateMission3Progress(HashMap<String, Object> progressMap) {
		session.update("nfctag.updateMission3Progress", progressMap);
	}

}
